import { getDataPlnel, getChart } from '@/api/dataPlatform'

const oldState = { // 用于重置state数据
  xData: [1, 2, 5, 7, 8, 11, 100],
  yData: ['2020-1-1', '2020-1-1', '2020-1-1', '2020-1-1', '2020-1-1', '2020-1-1', '2020-1-1'],
  panelData: [
    { id: 0, number: 123456, type: 'userCount', desc: '用户数量' },
    { id: 1, number: 122, type: 'meetingNumber', desc: '会议数量' },
    { id: 2, number: 123456, type: 'weekMeeting', desc: '本周会议' },
    { id: 3, number: 123456, type: 'mouthMeeting', desc: '本月会议' }

  ]
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_xData (state, data) {
    const xData = []
    data.map(item => {
      xData.push(item.conferenceNum)
    })
    state.xData = xData
  },
  set_yData (state, data) {
    const yData = []
    data.map(item => {
      yData.push(item.day)
    })
    state.yData = yData
  },
  set_panelData (state, data) {
    state.panelData[0].number = data.users
    state.panelData[1].number = data.conferences
    state.panelData[2].number = data.conferenceWeek
    state.panelData[3].number = data.monthConferences
  }
}
const actions = {
  // 获取看板数据
  getDataPlnelData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getDataPlnel(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_panelData', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },

  // 获取图标数据
  getChartData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getChart(payload).then(res => {
        if (res.data.code === 200 && res.data) {
          commit('set_xData', res.data.data)
          commit('set_yData', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
